/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.oper.snmp;

import hxg.portmgr.core.oper.snmp.oidgroup.OidEntry;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.*;

/**
 *
 * @author hxg
 */
public class SnmpOperUtils {

    private static final Logger logger = Logger.getLogger(SnmpOperUtils.class.getName());

    public static CommunityTarget getTarget(String ip, int port, String community, long timeout, int snmpVersion) {
        CommunityTarget target = new CommunityTarget();
        target.setCommunity(new OctetString(community));
        Address targetAddress = GenericAddress.parse("udp:" + ip + "/" + port);
        target.setAddress(targetAddress);
        target.setTimeout(timeout);
        target.setVersion(snmpVersion);
        return target;
    }

    public static boolean isReachAble(CommunityTarget target) {
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            PDU pdu = new PDU();
            pdu.setType(PDU.GETNEXT);
            pdu.add(new VariableBinding(new OID("1.3.6")));
            ResponseEvent response = snmp.send(pdu, target);
            //System.out.println(response);
            //if (snmp != null) {
            snmp.close();
            transport.close();
            //}
            if (response != null) {
                PDU rpdu = response.getResponse();
                return rpdu == null ? false : true;
                //return true;
            } else {
                return false;
            }
        } catch (IOException ex) {
            if (snmp != null) {
                try {
                    snmp.close();
                    if (transport != null) {
                        transport.close();
                    }
                } catch (IOException ex1) {
                    Logger.getLogger(SnmpOperUtils.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
            Logger.getLogger(SnmpOper.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public static boolean isReachAble(String ip, String commuity, long timeout) {
        CommunityTarget target = getTarget(ip, 161, commuity, timeout, SnmpConstants.version2c);
        return isReachAble(target);
    }

    public static CommunityTarget getTarget(String ip, String community) {
        return getTarget(ip, 161, community, 5000, SnmpConstants.version2c);
    }

    public static <T extends OidEntry> Map<T, Variable> getIntfTableVariblesOfGroupOids(String ifidx, T[] oidGroups, CommunityTarget target) throws IOException {
        return getIntfTableVariblesOfGroupOids(ifidx, oidGroups, 10, target);
    }

    public static <T extends OidEntry> Map<T, Variable> getIntfTableVariblesOfGroupOids(String ifidx, T[] oidGroups, int maxPduNum, CommunityTarget target) throws IOException {
        Map<T, Variable> mp = new HashMap<>();
        OID[] oids = new OID[oidGroups.length];
        for (int i = 0; i < oids.length; i++) {
            oids[i] = oidGroups[i].getOID().append(ifidx);
        }
        Map<OID, Variable> varmp = getVariablesofOIDs(oids, maxPduNum, target);
        //System.out.println(varmp);
        for (T entry : oidGroups) {
            if (ifidx != null && !ifidx.isEmpty()) {
                mp.put(entry, varmp.get(entry.getOID().append(ifidx)));
            } else {
                mp.put(entry, varmp.get(entry.getOID()));
            }
        }
        return mp;
    }

    public static <T extends OidEntry> Map<T, Variable>[] getIntfVariblesOfIntf(String[] ifidxs, T[] oidGroups, CommunityTarget target) throws IOException {

        Map<T, Variable>[] mps = new Map[ifidxs.length];
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            for (int i = 0; i < ifidxs.length; i++) {
                String ifidx = ifidxs[i];
                Map<T, Variable> mp = new HashMap<>();
                Map<OID, T> oid2param = new HashMap<>();
                PDU pdu = new PDU();
                OID ifidxOid = new OID(ifidx);
                pdu.setType(PDU.GET);
                for (T oidentry : oidGroups) {
                    OID oid = oidentry.getAppendOID(ifidxOid);
                    pdu.add(new VariableBinding(oid));
                    oid2param.put(oid, oidentry);
                }
                pdu = SnmpOperUtils.send(pdu, snmp, target);
                if (pdu != null) {
                    for (VariableBinding vb : pdu.toArray()) {
                        T t = oid2param.get(vb.getOid());
                        mp.put(t, vb.getVariable());
                    }
                }
                mps[i] = mp;
            }
            snmp.close();
            transport.close();
            return mps;
        } catch (IOException e) {
            if (snmp != null) {
                snmp.close();
                if (transport != null) {
                    transport.close();
                }
            }
            throw e;
        }
    }

    public static <T extends OidEntry> Map<T, Variable> getIntfVariblesOfIntf(String ifidx, T[] oidGroups, CommunityTarget target) throws IOException {
        Map<T, Variable> mp = new HashMap<>();
        Map<OID, T> oid2param = new HashMap<>();
        PDU pdu = new PDU();
        OID ifidxOid = new OID(ifidx);
        pdu.setType(PDU.GET);
        for (T oidentry : oidGroups) {
            OID oid = oidentry.getAppendOID(ifidxOid);
            pdu.add(new VariableBinding(oid));
            oid2param.put(oid, oidentry);
        }
        pdu = SnmpOperUtils.send(pdu, target);
        for (VariableBinding vb : pdu.toArray()) {
            T t = oid2param.get(vb.getOid());
            mp.put(t, vb.getVariable());
        }
        return mp;
    }

    public static Map<OID, Variable> getList(final OID oid, final CommunityTarget target) throws IOException {
        Map<OID, Variable> mp = new LinkedHashMap<>();
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            PDU pdu = new PDU();
            pdu.setType(PDU.GETNEXT);
            pdu.add(new VariableBinding(oid));
            ResponseEvent response = snmp.send(pdu, target);
            while (response != null) {
                pdu = response.getResponse();
                pdu.setType(PDU.GETNEXT);
                VariableBinding vb = pdu.get(0);
                if (!vb.isException()) {
                    OID roid = pdu.get(0).getOid();
                    if (roid.startsWith(oid)) {
                        OID ifOID = new OID(roid.getValue(), oid.size(), roid.size() - oid.size());
                        mp.put(ifOID, vb.getVariable());
                    } else {
                        try {
                            snmp.close();
                            transport.close();
                        } catch (IOException ex1) {
                            Logger.getLogger(SnmpOperUtils.class.getName()).log(Level.SEVERE, null, ex1);
                        }
                        return mp;
                    }
                    response = snmp.send(pdu, target);
                } else {
                    break;
                }
            }

            snmp.close();
        } catch (Exception e) {
            if (snmp != null) {
                try {
                    snmp.close();
                    if (transport != null) {
                        transport.close();
                    }
                } catch (IOException ex1) {
                    Logger.getLogger(SnmpOperUtils.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
            Logger.getLogger(SnmpOper.class.getName()).log(Level.SEVERE, null, e);
            throw e;
        }
        return mp;
    }

    /**
     * 返回一个<接口索引OID，<OID实体,值域>>组
     *
     * @param <T>
     * @param ifidxs 接口列表
     * @param oidGroups 采样的OidGroup
     * @param target
     * @return
     * @throws IOException
     */
    public static <T extends OidEntry> Map<OID, Map<T, Variable>> getTableVaribles(OID[] ifidxOids, T[] oidGroups, CommunityTarget target) throws IOException {
        Map<OID, Map<T, Variable>> mp = new HashMap<>();
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            for (OID ifoid : ifidxOids) {
                PDU pdu = new PDU();
                pdu.setType(PDU.GET);
                Map<OID, T> Oid2Tmap = new HashMap<>();
                for (T oidentry : oidGroups) {
                    OID oid = oidentry.getAppendOID(ifoid);
                    Oid2Tmap.put(oid, oidentry);
                    pdu.add(new VariableBinding(oid));
                }
                ResponseEvent response = snmp.send(pdu, target);
                Map<T, Variable> varmp = new HashMap<>();
                if (response != null) {
                    PDU retPdu = response.getResponse();
                    for (int i = 0; i < retPdu.size(); i++) {
                        VariableBinding vb = retPdu.get(i);
                        if (vb != null) {
                            OID oid = vb.getOid();
                            if (Oid2Tmap.containsKey(oid)) {
                                varmp.put(Oid2Tmap.get(oid), vb.getVariable());
                            }
                        }
                    }
                }
                mp.put(ifoid, varmp);
            }
            snmp.close();
            transport.close();
        } catch (IOException ex) {
            if (snmp != null) {
                try {
                    snmp.close();
                    if (transport != null) {
                        transport.close();
                    }
                } catch (IOException ex1) {
                    Logger.getLogger(SnmpOperUtils.class.getName()).log(Level.SEVERE, null, ex1);
                }
            }
            Logger.getLogger(SnmpOper.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
            //return false;
        }
        return mp;
    }

    public static <T extends OidEntry> Map<OID, Map<T, Variable>> getVariblesOfOidEntryByRow(T[] oidGroups, CommunityTarget target) throws IOException {
        Map<OID, Map<T, Variable>> resmp = new LinkedHashMap<>();
        if (oidGroups.length == 0) {
            return resmp;
        }
        PDU pdu = new PDU();
        pdu.setType(PDU.GETNEXT);
        for (OidEntry oidentry : oidGroups) {
            pdu.add(new VariableBinding(oidentry.getOID()));
        }
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            //int firstprefixlenth = oidGroups[0].getOID().size();
            while (true) {
                //System.out.println(pdu);
                pdu = send(pdu, snmp, target);
                //System.out.println(pdu);
                if (pdu == null || pdu.getVariableBindings().isEmpty()) {
                    break;
                }
                pdu.setType(PDU.GETNEXT);
                OID firstOId = pdu.get(0).getOid();
                //String firstoidstr = pdu.get(0).getOid().toString();
                if (!firstOId.startsWith(oidGroups[0].getOID())) {
                    break;
                }
                String ifidx = firstOId.toString().substring(oidGroups[0].getOID().toString().length());
                OID ifidxOId = new OID(ifidx);
                Map<T, Variable> mp = new LinkedHashMap<>();
                Map<OID, Variable> oid2varMp = new HashMap<>();
                for (VariableBinding vb : pdu.getVariableBindings()) {
                    oid2varMp.put(vb.getOid(), vb.getVariable());
                }
                for (T oidentry : oidGroups) {
                    OID oid = oidentry.getAppendOID(ifidxOId);
                    if (oid2varMp.containsKey(oid)) {
                        mp.put(oidentry, oid2varMp.get(oid));
                    } else {
                        break;
                    }
                }
                if (mp.size() != oidGroups.length) {
                    break;
                }
                resmp.put(ifidxOId, mp);
            }
            snmp.close();
            transport.close();
            return resmp;
            //return rpdu;
        } catch (IOException e) {
            if (snmp != null) {
                snmp.close();
                if (transport != null) {
                    transport.close();
                }
            }
            throw e;
        }

    }

    public static Map<OID, Variable> getVariablesofOIDs(OID[] oids, CommunityTarget target) throws IOException {
        return getVariablesofOIDs(oids, 10, target);
    }

    public static Map<OID, Variable> getVariablesofOIDs(OID[] oids, int maxNumPerPDU, CommunityTarget target) throws IOException {
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            //Map<String, Map<OID, Variable>> mp = new LinkedHashMap<String, Map<OID, Variable>>();
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            Map<OID, Variable> mp = new LinkedHashMap();
            PDU pdu = new PDU();
            pdu.setType(PDU.GET);
            for (OID oid : oids) {
                pdu.addOID(new VariableBinding(oid));
            }
            ResponseEvent response = snmp.send(pdu, target);
            if (response != null) {
                PDU recv_pdu = response.getResponse();
                if (recv_pdu != null && recv_pdu.size() > 0) {
                    for (int i = 0; i < recv_pdu.size(); i++) {
                        try {
                            OID key = recv_pdu.get(i).getOid();
                            Variable var = recv_pdu.get(i).getVariable();
                            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{key, var});
                            mp.put(key, var);
                        } catch (Exception e) {
                            logger.log(Level.WARNING, "发送时异常 {}", new Object[]{e});
                        }
                    }
                }
            }
//            if (snmp != null) {
            transport.close();
            snmp.close();
//            }
            return mp;
        } catch (IOException ex) {
            if (snmp != null) {
                if (transport != null) {
                    transport.close();
                }
                snmp.close();






            }
            Logger.getLogger(SnmpOperUtils.class
                    .getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
    }

    /**
     * 返回〈Index索引OID,<原目标OID,Varible>〉
     *
     * @param oids
     * @param target
     * @return
     * @throws IOException
     */
    public static Map<OID, Map<OID, Variable>> getVariablelTableOfOIDs(OID[] oids, CommunityTarget target) throws IOException {
        return getVariablelTableOfOIDs(oids, new int[]{6, 10}, target);
    }

    public static Map<OID, Map<OID, Variable>> getVariablelTableOfOIDs(OID[] oids, int[] rowAndColumnMaxNumPerPDU, CommunityTarget target) throws IOException {
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            Map<OID, Map<OID, Variable>> mp = new LinkedHashMap<>();

            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            PDUFactory fac = new DefaultPDUFactory(PDU.GETNEXT);
            TableUtils tu = new TableUtils(snmp, fac);
            if (rowAndColumnMaxNumPerPDU != null) {
                switch (rowAndColumnMaxNumPerPDU.length) {
                    case 0:
                        break;
                    case 1:
                        tu.setMaxNumRowsPerPDU(rowAndColumnMaxNumPerPDU[0]);
                        break;
                    case 2:
                        tu.setMaxNumRowsPerPDU(rowAndColumnMaxNumPerPDU[0]);
                        tu.setMaxNumRowsPerPDU(rowAndColumnMaxNumPerPDU[1]);
                        break;
                    default:
                        tu.setMaxNumRowsPerPDU(rowAndColumnMaxNumPerPDU[0]);
                        tu.setMaxNumRowsPerPDU(rowAndColumnMaxNumPerPDU[1]);
                        break;
                }
            }
            List list = tu.getTable(target, oids, null, null);
            for (int i = 0; i < list.size(); i++) {
                TableEvent te = (TableEvent) list.get(i);
                if (te.getIndex() == null) {
                    //如果index返回的是null，这个返回一个空Map
                    snmp.close();
                    transport.close();
                    return mp;
                }
                //String index = te.getIndex().toString();
                VariableBinding[] vb = te.getColumns();
                if (vb != null) {
                    Map<OID, Variable> varmp = new LinkedHashMap<>();
                    for (int j = 0; j < vb.length; j++) {
                        if (vb[j] != null) {
                            varmp.put(oids[j], vb[j].getVariable());
                        }
                    }
                    mp.put(te.getIndex(), varmp);
                }
            }
            snmp.close();
            transport.close();
            return mp;
        } catch (IOException ex) {
            if (snmp != null) {
                snmp.close();
                if (transport != null) {
                    transport.close();
                }
            }
            throw ex;
        }
    }

    public static PDU send(final PDU pdu, CommunityTarget target) throws IOException {
        TransportMapping transport = null;
        Snmp snmp = null;
        try {
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            PDU rpdu = send(pdu, snmp, target);
            snmp.close();
            transport.close();
            return rpdu;
        } catch (IOException e) {
            if (snmp != null) {
                snmp.close();
                if (transport != null) {
                    transport.close();
                }
            }
            throw e;
        }
    }

    public static PDU send(final PDU pdu, final Snmp snmp, CommunityTarget target) throws IOException {
        ResponseEvent response = snmp.send(pdu, target);
        if (response != null) {
            PDU recv_pdu = response.getResponse();
            return recv_pdu;
        }
        return null;
    }

    public static Variable get(OID oid, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.GET);
        pdu.add(new VariableBinding(oid));
        pdu = send(pdu, target);
        if (pdu != null && pdu.size() > 0) {
            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{oid, pdu.get(0).getVariable()});
            return pdu.get(0).getVariable();
        } else {
            logger.log(Level.FINE, "发送：{0};未接收到数据", new Object[]{oid});
            return null;
        }
    }

    public static Variable[] get(OID[] oids, int OidsEachPdu, Snmp snmp, CommunityTarget target) throws IOException {
        int local = 0;
        Variable[] vars = new Variable[oids.length];
        PDU pdu;
        while (local < oids.length) {
            pdu = new PDU();
            int count = 0;
            while (count < OidsEachPdu && local < oids.length) {
                pdu.add(new VariableBinding(oids[local]));
                count++;
                local++;
            }
            ResponseEvent response = snmp.send(pdu, target);
            if (response != null) {
                PDU recv_pdu = response.getResponse();
                logger.log(Level.FINE, "PDU={0}\nREC_PDU={1}", new Object[]{pdu, recv_pdu});
                if (recv_pdu != null) {
                    List<? extends VariableBinding> vbs = recv_pdu.getVariableBindings();
                    for (int i = 0; i < count; i++) {
                        vars[local - count + i] = vbs.get(i).getVariable();
                    }
                }
            }
        }
        return vars;
    }

    public static Variable[] get(OID[] oids, int OidsEachPdu, CommunityTarget target) throws IOException {
        TransportMapping transport = null;
        Snmp snmp = null;
        //int local = 0;
        Variable[] vars = new Variable[oids.length];
        try {

            PDU pdu;
            transport = new DefaultUdpTransportMapping();
            transport.listen();
            snmp = new Snmp(transport);
            vars = get(oids, OidsEachPdu, snmp, target);
//            while (local < oids.length) {
//                pdu = new PDU();
//                int count = 0;
//                while (count < OidsEachPdu && local < oids.length) {
//                    pdu.add(new VariableBinding(oids[local]));
//                    count++;
//                    local++;
//                }
//                ResponseEvent response = snmp.send(pdu, target);
//                if (response != null) {
//                    PDU recv_pdu = response.getResponse();
//                    logger.log(Level.FINE, "PDU={0}\nREC_PDU={1}", new Object[]{pdu, recv_pdu});
//                    if (recv_pdu != null) {
//                        List<? extends VariableBinding> vbs = recv_pdu.getVariableBindings();
//                        for (int i = 0; i < count; i++) {
//                            vars[local - count + i] = vbs.get(i).getVariable();
//                        }
//                    }
//                }
//
//            }
            snmp.close();
            transport.close();
        } catch (IOException e) {
            logger.log(Level.SEVERE, null, e);
            System.out.println(e.getMessage());
            if (snmp != null) {
                snmp.close();
                if (transport != null) {
                    transport.close();
                }
            }
            throw e;
        }
        return vars;
    }

    public static Variable get(String oid, final Snmp snmp, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.GET);
        pdu.add(new VariableBinding(new OID(oid)));
        pdu = send(pdu, snmp, target);
        if (pdu != null && pdu.size() > 0) {
            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{oid, pdu.get(0).getVariable()});
            return pdu.get(0).getVariable();
        } else {
            logger.log(Level.FINE, "发送：{0};未接收到数据", new Object[]{oid});
            return null;
        }
    }

    public static Variable get(String oid, CommunityTarget target) throws IOException {
        return get(new OID(oid), target);
//        PDU pdu = new PDU();
//        pdu.setType(PDU.GET);
//        pdu.add(new VariableBinding(new OID(oid)));
//        pdu = send(pdu, target);
//        if (pdu != null && pdu.size() > 0) {
//            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{oid, pdu.get(0).getVariable()});
//            return pdu.get(0).getVariable();
//        } else {
//            logger.log(Level.FINE, "发送：{0};未接收到数据", new Object[]{oid});
//            return null;
//        }
    }

    public static Variable set(VariableBinding vb, Snmp snmp, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        pdu.add(vb);
        pdu = send(pdu, target);
        if (pdu != null && pdu.size() > 0) {
            Variable varb = pdu.get(0).getVariable();
            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{vb.getVariable(), varb.toString()});
            return varb;
        } else {
            return null;
        }
    }

    public static String set(VariableBinding[] vbs, CommunityTarget target) throws IOException {

        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        pdu.addAll(vbs);
        pdu = send(pdu, target);
        if (pdu != null && pdu.size() > 0) {
            String val = pdu.get(0).getVariable().toString().trim();
            logger.log(Level.FINE, "批量Set Info:", new Object[]{pdu.toString()});

            return val;
        } else {

            return "";
        }
    }

    public static String set(String oid, int value, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        pdu.add(new VariableBinding(new OID(oid), new Integer32(value)));
        pdu = send(pdu, target);
        if (pdu != null && pdu.size() > 0) {
            String val = pdu.get(0).getVariable().toString().trim();
            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{oid, val});
            return val;
        } else {
            return "";
        }
    }

    public static String set(OID oid, int value, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        pdu.add(new VariableBinding(oid, new Integer32(value)));
        pdu = send(pdu, target);
        if (pdu != null && pdu.size() > 0) {
            String val = pdu.get(0).getVariable().toString().trim();
            logger.log(Level.FINE, "发送：{0};接收到：{1}", new Object[]{oid, val});
            return val;
        } else {
            return "";
        }
    }

    public static String[] set(String[] oids, Object[] values, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        for (int i = 0; i < oids.length; i++) {
            if (values[i] instanceof Integer) {
                int val = Integer.parseInt(values[i].toString());
                pdu.add(new VariableBinding(new OID(oids[i]), new Integer32(val)));
            } else {
                pdu.add(new VariableBinding(new OID(oids[i]), new OctetString(values[i].toString())));
            }
            logger.log(Level.FINE, "SET:发送：{0} 设置值:{1};", new Object[]{oids[i], values[i]});
        }
        PDU recv_pdu = send(pdu, target);
        if (recv_pdu != null && recv_pdu.size() > 0) {
            String[] ress = new String[recv_pdu.size()];
            for (int i = 0; i < ress.length; i++) {
                ress[i] = recv_pdu.get(i).getVariable().toString();
                logger.log(Level.FINE, "SET:接收到：{0}", new Object[]{ress[i]});
            }
            return ress;
        } else {
            logger.log(Level.FINE, "没有接收到数据");
            return new String[0];
        }
    }

    public static String set(String oid, String value, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        pdu.add(new VariableBinding(new OID(oid), new OctetString(value)));
        logger.log(Level.FINE, "SET:发送：{0} 设置值:{1};", new Object[]{oid, value});
        PDU recv_pdu = send(pdu, target);
        if (recv_pdu != null && recv_pdu.size() > 0) {
            String res = recv_pdu.get(0).getVariable().toString();
            logger.log(Level.FINE, "SET:接收到:{0};", new Object[]{res});
            return res;
        } else {
            logger.log(Level.FINE, "没有接收到数据");
            return "";
        }
    }

    public static String set(OID oid, String value, CommunityTarget target) throws IOException {
        PDU pdu = new PDU();
        pdu.setType(PDU.SET);
        pdu.add(new VariableBinding(new OID(oid), new OctetString(value)));
        logger.log(Level.FINE, "SET:发送：{0} 设置值:{1};", new Object[]{oid, value});
        PDU recv_pdu = send(pdu, target);
        if (recv_pdu != null && recv_pdu.size() > 0) {
            String res = recv_pdu.get(0).getVariable().toString();
            logger.log(Level.FINE, "SET:接收到:{0};", new Object[]{res});
            return res;
        } else {
            logger.log(Level.FINE, "没有接收到数据");
            return "";
        }

    }

    /**
     * 可用，用对Dslam添加速率模板功能测试通过，但是附加的参数测试没有成功，需要其他的进一步测试
     *
     * @param rowStatusColumnOID
     * @param rowIndex
     * @param values
     * @param target
     * @return
     * @throws IOException
     */
    public static ResponseEvent createRow(OID rowStatusColumnOID, OID rowIndex, VariableBinding[] values, CommunityTarget target) throws IOException {
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();
        Snmp snmp = new Snmp(transport);
        PDUFactory fac = new DefaultPDUFactory(PDU.GETNEXT);
        TableUtils tu = new TableUtils(snmp, fac);
        ResponseEvent createRow = tu.createRow(target, rowStatusColumnOID, rowIndex, values);
        snmp.close();
        transport.close();
        return createRow;
    }

    public static void destoryRow(OID rowStatusColumnOID, OID rowIndex, CommunityTarget target) throws IOException {
        TransportMapping transport = new DefaultUdpTransportMapping();
        transport.listen();
        Snmp snmp = new Snmp(transport);
        PDUFactory fac = new DefaultPDUFactory(PDU.GETNEXT);
        TableUtils tu = new TableUtils(snmp, fac);
        ResponseEvent createRow = tu.destroyRow(target, rowStatusColumnOID, rowIndex);
        snmp.close();
        transport.close();
    }
}
